package org.cid54.jshopper.service.impl;

import java.util.List;
import org.cid54.jshopper.dao.ItemDao;
import org.cid54.jshopper.dao.ShopItemDao;
import org.cid54.jshopper.dao.ShoppingListDao;
import org.cid54.jshopper.dao.UserDao;
import org.cid54.jshopper.model.Item;
import org.cid54.jshopper.model.ShopItem;
import org.cid54.jshopper.model.ShoppingList;
import org.cid54.jshopper.model.User;
import org.cid54.jshopper.service.ShopItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author fgo
 */
@Service
@Transactional
public class ShopItemServiceImpl implements ShopItemService {

    @Autowired
    private ItemDao itemDao;
    @Autowired
    private ShopItemDao shopItemDao;
    @Autowired
    private ShoppingListDao shoppingListDao;
    @Autowired
    private UserDao userDao;

    @Override
    public List<Item> getItems() {
        return this.itemDao.getAll();
    }

    @Override
    public ShopItem getShopItemByName(String shopItemName) {
        return this.shopItemDao.findByName(shopItemName);
    }

    @Override
    public void create(ShopItem shopItem, String userName) {

        User user = this.userDao.findByName(userName);

        ShoppingList shoppingList = this.shoppingListDao.findByName(shopItem.getShoppingList().getName());

        if (user.getUserGroups().contains(shoppingList.getUserGroup())) {

            Item item = this.itemDao.findByName(shopItem.getItem().getName());

            ShopItem dbItem = this.shopItemDao.findByItemAndShoppingList(item, shoppingList);

            if (dbItem != null) {

                dbItem.setAmount(dbItem.getAmount() + shopItem.getAmount());

                this.shopItemDao.update(dbItem);

            } else {

                shopItem.setShoppingList(shoppingList);
                shopItem.setItem(item);

                this.shopItemDao.create(shopItem);

            }
        }

    }

    @Override
    public void update(ShopItem item, String userName) {
        User user = this.userDao.findByName(userName);

        ShoppingList shoppingList = this.shoppingListDao.findByName(item.getShoppingList().getName());

        if (user.getUserGroups().contains(shoppingList.getUserGroup())) {

            ShopItem dbItem = this.shopItemDao.findById(item.getId());

            dbItem.setAmount(item.getAmount());

            this.shopItemDao.update(dbItem);
        }
    }

    @Override
    public void toggleBoughtStatus(long shopItemId) {

        ShopItem shopItem = this.shopItemDao.findById(shopItemId);

        shopItem.setBought(!shopItem.isBought());

        this.shopItemDao.update(shopItem);

    }

    @Override
    public void deleteShopItem(long shopItemId) {

        ShopItem shopItem = this.shopItemDao.findById(shopItemId);

        this.shopItemDao.delete(shopItem);
    }

    @Override
    public void createItem(Item item) {

        this.itemDao.create(item);

    }
}
